package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"shops/models"
	"shops/pkg"
)

type UserController struct {
	beego.Controller
}

type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"info"`
	Data interface{} `json:"data"`
}

func (c *UserController) Login() {
	username := c.GetString("username")
	password := c.GetString("password")
	var user models.User
	if err := models.DB.Model(models.User{}).Where("username = ?", username).First(&user).Error; err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "账号不存在",
		}
		c.ServeJSON()
		return
	}
	if pkg.MD5(password) != user.Password {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "密码错误",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = JsonRes{
		Code: 200,
		Msg:  "登录成功",
	}
	c.ServeJSON()
	return
}
